package com.zbbt.sportslottery.util;

import android.content.Context;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.zbbt.sportslottery.app.App;
import com.zbbt.sportslottery.app.Constant;

public class LocationUtil {
	public static final String TAG = "LocationUtil";
	private static LocationClient mLocationClient;
	private static String province="";// 省份
	private static Double Longitude = 0.0;// 定位获取经度
	private static Double Latitude = 0.0;// 定位获取纬度

	public static String getProvince(Context context) {
		mLocationClient = new LocationClient(context);
		LocationClientOption option = new LocationClientOption();// 打开经纬度坐标
		option.setLocationMode(LocationMode.Hight_Accuracy);// 高精度模式
		option.setOpenGps(true);// 打开gps
		option.setCoorType("bd09ll"); // 设置坐标类型
		option.setIsNeedAddress(true);// 设置详细地址
		option.setScanSpan(1000);
		mLocationClient.setLocOption(option);
		mLocationClient.registerLocationListener(new BDLocationListener() {

			@Override
			public void onReceiveLocation(BDLocation location) {
				province = location.getProvince();
				Longitude = location.getLongitude();
				Latitude = location.getLatitude();
				String oldProvince = SharePreferUtil.getString(Constant.CITY_NAME_LOCATION, "");
				if (Latitude != 4.9E-324 && Latitude != 0.0) {
					if (!"".equals(oldProvince)&&province!=null) {
						if (!oldProvince.equals(province)) {
							if(!province.equals(SharePreferUtil.getString(Constant.CITY_NAME, ""))){
								App.isChangeLocation=true;//用来首页判断
							}
						}
					}
					SharePreferUtil.putString(Constant.CITY_NAME_LOCATION, province);
					App.cityName=province;
					SharePreferUtil.putString("Longitude", Longitude + "");
					SharePreferUtil.putString("Latitude", Latitude + "");
				}
				if (mLocationClient != null) {
					mLocationClient.stop();
				}
				LogUtil.printI("aaaaaaaaaa", "省份：" + province + "。 经度：" + Longitude + "。 纬度：" + Latitude);
			}
		});
		mLocationClient.start();
		return province;
	}
}
